/**
 * author: kapil kashyap
 */

package com.java.practise.tags;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.jsp.tagext.TagSupport;

@SuppressWarnings("serial")
public class Base extends TagSupport{
	protected static final String BREAD_CRUMB_LITERAL 			= "&p=";
	protected static final String BREAD_CRUMB_LITERAL_2 		= "p=";
	
	protected static final String DEFAULT_FONT 					= "font-family: verdana; ";
	protected static final String DEFAULT_FONT_SIZE 			= "font-size: 13px; ";
	protected static final String DEFAULT_BORDER				= "border: 1px solid #cccccc; ";
	protected static final String DEFAULT_PADDING 				= "padding: 2px; ";
	protected static final String LINK_POINTER 					= "cursor: pointer; ";
	protected static final String TEXT_TRANSFORM 				= "text-transform: capitalize; ";
	protected static final String TEXT_DECORATION 				= "text-decoration: none; ";
	
	protected static final String DEFAULT_CSS_STYLE 			= DEFAULT_FONT + DEFAULT_FONT_SIZE + DEFAULT_BORDER;
	protected static final String DEFAULT_HEADER_CSS_STYLE 		= DEFAULT_CSS_STYLE + TEXT_TRANSFORM;
	protected static final String DEFAULT_LINK_STYLE 			= DEFAULT_FONT + DEFAULT_FONT_SIZE + DEFAULT_PADDING + TEXT_DECORATION;

	protected ResultSet rst 									= null;
	protected boolean select 									= false;
	protected boolean rowCountRequired 							= false;
	protected ArrayList<Object> collection 						= null;
	protected String[] columnNames 								= null;

	protected String headerStyle 								= "";	
	protected String rowStyle									= "";	
	protected String pageMarkupStyle 								= "";
	protected String footerStyle 								= "";
	
	protected String defaultHeaderCssStyle() {
		return DEFAULT_HEADER_CSS_STYLE;
	}

	protected String defaultRowCssStyle() {
		return DEFAULT_CSS_STYLE;
	}
	
	protected String defaultFooterCssStyle() {
		return DEFAULT_CSS_STYLE;
	}
	
	protected String defaultLinkStyle() {
		return DEFAULT_LINK_STYLE;	
	}
	
	public void setRst(ResultSet rst) {
		this.rst = rst;
	}

	public void setCollection(ArrayList<Object> collection) {
		this.collection = collection;
	}

	public void setColumnNames(String[] columnNames) {
		this.columnNames = columnNames;
	}
	
	public void setSelect(boolean flag) {
		this.select = flag;
	}	
	
	public void setRowCountRequired(boolean rowCountRequired) {
		this.rowCountRequired = rowCountRequired;
	}
	
	public void setHeaderStyle(String headerStyle) {
		this.headerStyle = headerStyle;
	}
	
	public void setRowStyle(String rowStyle) {
		this.rowStyle = rowStyle;
	}
	
	public void setFooterStyle(String footerStyle) {
		this.footerStyle = footerStyle;
	}
	
	public void setPageMarkupStyle(String pageMarkupStyle) {
		this.pageMarkupStyle = pageMarkupStyle;
	}
	
	protected int getRowCount(ResultSet rst) throws SQLException {
		int noOfRows = 0;
		try {
		    while(rst.next()) {
		    	noOfRows++;
			}
		    rst.beforeFirst();
		}
		catch(SQLException ex) {
			throw new SQLException("SQLException occured while forming the paginated table: " + ex.getMessage());
		}
		return noOfRows;
	}
	
	protected StringBuffer parseXMLString(String data){
		StringBuffer stringBuffer = new StringBuffer();
		for(int j = 0; j < data.length(); j++){
			if(data.charAt(j) == '<'){
				stringBuffer.append("&#60;");
			}
			else if(data.charAt(j) == '?'){
				stringBuffer.append("&#63;");			    					
			}
			else if(data.charAt(j) == '!'){
				stringBuffer.append("&#33;");			    					
			}
			else if(data.charAt(j) == '"'){
				stringBuffer.append("&#34;");			    					
			}
			else if(data.charAt(j) == '>'){
				stringBuffer.append("&#62;");			    					
			}
			else if(data.charAt(j) == '/'){
				stringBuffer.append("&#47;");			    					
			}
			else{
				stringBuffer.append(data.charAt(j));
			}
		}
		return stringBuffer;
	}
}
